--[[
 -- *名称:tablepart
 -- *描述:桌子
 -- *版权:Copyright © 2016-2018 深圳市三只小熊科技有限公司 版权所有
 -- *作者:Bryan.Chen
 -- *创建日期:2017-09-30
 -- *修改日期:2017-09-30
 -- @return [description]
 ]]
local CURRENT_MODULE_NAME = ...
local TablePart = class("TablePart", PokerPartBase)
TablePart.DEFAULT_VIEW = "tablescene"
TablePart.DEFAULT_PART = {
	"BgLayerPart",
	"GameLayerPart",
	"EffectLayerPart",
	"PopupLayerPart",
	"SystemPopupLayerPart",
}
---
 -- 构造方法
 -- @DateTime 2017-11-06
 -- @param    owner [description]
 -- @return    [description]
 --
function TablePart:ctor(owner)
	Log.d("TablePart:ctor");
    TablePart.super.ctor(self, owner)
    self:_initialize();
end
---
 -- 初始化
 -- @DateTime 2017-11-14
 -- @return   [description]
 --
function TablePart:_initialize(  )
	cc.bind(self, "roomlogic");

    self._frameDataArr = {};
    self._frameHandlerTime = 0;
    self._delayConfig = {
    	[DdzGameConfig.TableState.GAME_START]	= 4.0;
    };
end
---
 -- 激活组件
 -- @DateTime 2017-11-06
 -- @param    data [description]
 -- @return    [description]
 --
function TablePart:activate(data)
	if IOS_BACK_DELAY == true then
		IOS_BACK_DELAY = false
		local lua_bridge = global:getModuleWithId(ModuleDef.BRIDGE_MOD)
		if lua_bridge.setBackDelayTime then
			lua_bridge:setBackDelayTime(10)
		end
	end

	TablePart.super.activate(self, CURRENT_MODULE_NAME)

	self:activateSubParts(data);	-- 激活子组件
	-- 开始轮循处理帧协议延时
	if not self._frameHandlerEntryId then
		self:startFrameHandler(); 
	end

	SoundMgr.playMusic(SoundMgr.SoundId.BGM);

	self:_checkNetworkState();
	Toast.showText("按esc 返回子游戏大厅");
end
---
 -- 检查网络状态 
 -- @DateTime 2017-11-23
 -- @return   [description]
 --
function TablePart:_checkNetworkState(  )
 	local temp,handlerIndex =	global:getNetManager():addEventListener("onNotify", handler(self, self._checkNetworkStateCallback),"DDZTablePartonNotify");
 	self.handlerIndex = handlerIndex ;
end
---
 -- 网络状态回调方法
 -- @DateTime 2017-11-23
 -- @param    event [description]
 -- @return    [description]
 --
function TablePart:_checkNetworkStateCallback( event )
	if event.code == NET_STATE.CONNECT_CHECK_OK then --检测网络连接有效
		FrameUtils.removeEnterFrameListener(self._checkNetworkEntryId);
		self._checkNetworkEntryId = nil;
		-- -- 请求加入游戏
		-- self:requestLoginGame(DdzGameConfig.GAME_ID);
	elseif event.code == NET_STATE.CONNECT_FAIL then --网络连接失败
		-- local str = "您的网络已断开，我们正在尝试重连...";
		-- Toast.showText(str);
		-- local str = "网络连接失败！";
		-- Toast.showText(str, 10);
	elseif event.code == NET_STATE.CONNECT_RECONNECT then --网络重新连接
		if self._checkNetworkEntryId then 
			return;
		end
		self._checkNetworkEntryId = FrameUtils.addEnterFrameListener(function()
			local str = "您的网络已断开，我们正在尝试重连...";
			Toast.showText(str, 10);
		end, 10);
	end	
end

---
 -- 激活子组件
 -- @DateTime 2017-11-06
 -- @return   [description]
 --
function TablePart:activateSubParts( data )
	self:activatePart("BgLayerPart", data);
	self:activatePart("GameLayerPart", data);
	self:activatePart("EffectLayerPart");
	self:activatePart("PopupLayerPart");
	self:activatePart("SystemPopupLayerPart");
end
---
 -- 反激活组件
 -- @DateTime 2017-11-06
 -- @return   [description]
 --
function TablePart:deactivate()

	global:getNetManager():removeEventListener(self.handlerIndex);
	FrameUtils.removeEnterFrameListener(self._frameHandlerEntryId);
	self._frameHandlerEntryId = nil;
	SoundMgr.stopMusic();
	TablePart.super.deactivate(self);
end
---
 -- 获取partId
 -- @DateTime 2017-11-06
 -- @return   [description]
 --
function TablePart:getPartId()
	return "TablePart"
end

---
 -- 开始轮循处理帧协议延时
 -- @DateTime 2017-11-06
 -- @return   [description]
 --
function TablePart:startFrameHandler(  )
	self._frameHandlerEntryId = FrameUtils.addEnterFrameListener(function()
		if not self._isDipatching then 
			if #self._frameDataArr > 0 then 
				local frameData = table.remove(self._frameDataArr, 1);
				TablePart.super.onFrameData(self, frameData);
				local delayTime = self._delayConfig[frameData.tableInfo.tableState] or 0;
				if delayTime > 0 then
					self._isDipatching = true; 
					self._delayDispatchEntryId = FrameUtils.addEnterFrameListener(function()
						FrameUtils.removeEnterFrameListener(self._delayDispatchEntryId);
						self._isDipatching = false;
					end, delayTime);
				end
			end
		end
	end, 0);
end


---
 -- 返回子游戏大厅
 -- @DateTime 2017-11-06
 -- @return    [description]
 --
function TablePart:returnLobby( )
	self:startLoading();
	local net_mode = global:getModuleWithId(ModuleDef.NET_MOD)
	local opt_msg = ddzCommon_pb.PlayerGameOpertaion()
	opt_msg.opid = RoomConfig.GAME_OPERATION_PLAYER_LEFT_TABLE
	net_mode:sendProtoMsg(opt_msg,SocketConfig.MSG_GAME_OPERATION,DdzGameConfig.GAME_ID)
	self:returnGame()
end
---
 -- 返回子游戏大厅
 -- @DateTime 2017-11-06
 -- @return   [description]
 --
function TablePart:returnGame()

	self:release();
	local net_mode = global:getModuleWithId(ModuleDef.NET_MOD)
	net_mode:refreshSeq()
	--清除缓存
	CleanUtils.clean(GamePath);
	--重新加载库
	require(GamePath .. ".config.ddzloadpackage");
	local user = global:getGameUser()
	local lobby_part = global:createPart("LobbyPart",user);
	lobby_part:activate(DdzGameConfig.GAME_ID);
end    


function TablePart:_onReconnectAck( data )
	SoundMgr.playMusic(SoundMgr.SoundId.BGM);
	local fnSuccess = function ( loginAckData )
		-- 断线重连登录成功
		Log.d("TablePart:_onReconnectAck gameId: ", loginAckData.playerInfo.gameId);
		Log.d("TablePart:_onReconnectAck gameId2: ", DdzGameConfig.GAME_ID);
		Log.d("TablePart:_onReconnectAck gameId3: ", DdzGameConfig.GAME_ID == loginAckData.playerInfo.gameId);
		self:requestStartGame(DdzGameConfig.GAME_ID);
	end
	local fnFailed = function ( loginAckData )
		--断线重连登录不成功切换到登录界面
		self:returnLobby();
	end
	Toast.showText("您的网络已断开，我们正在尝试重连...");
	self:handleReconnectAck(data, fnSuccess, fnFailed);
	Log.d("TablePart:_onReconnectAck");
	-- self:_onEnterRoomAck(data);
end

--[[
 -- 退出房间
 -- @DateTime 2017-11-28
 -- @return   [description]
 ]]
function TablePart:_onExit(  )
	xxtimer.delay(0.1, function()
		self:returnLobby();	
	end);
end

---
 -- 事件监听配置
 -- @type {Object}
 --
TablePart.eventFuncMap = {
   [DdzEventConfig.EXIT] 							= TablePart._onExit;
};

return TablePart